package com.springcloud.security;

import com.springcloud.commons.result.Result;
import com.springcloud.commons.utils.StringUtils;
import com.springcloud.jwt.JwtConfigProperty;
import com.springcloud.jwt.JwtUtil;
import org.codehaus.jackson.map.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.security.web.authentication.logout.LogoutHandler;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 自定义的注销处理类
 */
@Component
public class MyLogoutSuccessHandler implements LogoutSuccessHandler {

    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private JwtConfigProperty jwtConfig;
    @Autowired
    private JwtUtil jwtUtil;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Override
    public void onLogoutSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        String authHeader = "";
        Cookie[] cookies = httpServletRequest.getCookies();
        if (cookies!=null) {
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                if (cookie.getName().equals(jwtConfig.getHeader())) {
                    authHeader = cookie.getValue();
                    break;
                }
            }
        }
        if(StringUtils.isBlank(authHeader)) {
            authHeader = httpServletRequest.getHeader(jwtConfig.getHeader());//获取浏览器传过来的token信息
        }
        String authToken = authHeader.substring(jwtConfig.getTokenHead().length());//去掉开头
        String username = jwtUtil.getSubjectFromToken(authToken);
        logger.info("注销:"+username);
        if(StringUtils.isNotBlank(stringRedisTemplate.opsForValue().get(username))){
            stringRedisTemplate.delete(username);
        }
        httpServletResponse.setHeader(jwtConfig.getHeader(), "");
        Cookie cookie = new Cookie(jwtConfig.getHeader(), "");
        httpServletResponse.addCookie(cookie);
        Result result = new Result();
        result.setMsg("退除系统成功！");
        ObjectMapper mapper = new ObjectMapper();
        String data = mapper.writeValueAsString(result);
        httpServletResponse.getWriter().write(data);
    }
}
